//
//  BabyHelperAppDelegate.m
//  BabyHelper
//
//  Created by  thomas on 11-2-11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import "BabyHelperAppDelegate.h"
#import "Connections.h"

@implementation BabyHelperAppDelegate

@synthesize window;
@synthesize tabBarController;
@synthesize appKey;
@synthesize appSecret;
@synthesize tokenKey;
@synthesize tokenSecret;
@synthesize verifier;
@synthesize response;
@synthesize bindStatus;
@synthesize bindName;
@synthesize _engine;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
	
	PhotoManager *photoManager = [PhotoManager getInstance];
    [self loadDefaultKey];
	if (!_engine){

		OAToken *savedAccessToken = [[OAToken alloc] initWithUserDefaultsUsingServiceProviderName:@"SINA" prefix:@"ACCESS_TOKEN"];
		_engine = [[OAuthEngine alloc] initOAuthWithDelegate: self];
		_engine.accessToken    = savedAccessToken;
		_engine.consumerKey    = SINA_APP_KEY;
		_engine.consumerSecret = SINA_APP_SECRET;
		_engine.requestTokenURL = [NSURL URLWithString:SINA_REQUEST_TOKEN_URL];
		_engine.accessTokenURL  = [NSURL URLWithString:SINA_ACCESS_TOKEN_URL];
		_engine.authorizeURL    = [NSURL URLWithString:SINA_AUTHORIZE_URL];

		
/*		
		OAToken *savedAccessToken = [[OAToken alloc] initWithUserDefaultsUsingServiceProviderName:@"QQ" prefix:@"ACCESS_TOKEN"];
		_engine = [[OAuthEngine alloc] initOAuthWithDelegate: self];
		_engine.accessToken     = savedAccessToken;
		//OAToken *requestToken   = [[OAToken alloc] initWithKey:TECENT_APP_KEY secret:TECENT_APP_SECRET];
		//_engine.requestToken    = requestToken;
		_engine.consumerKey     = TECENT_APP_KEY;
		_engine.consumerSecret  = TECENT_APP_SECRET;
		_engine.requestTokenURL = [NSURL URLWithString:QQ_REQUEST_TOKEN_URL];
		_engine.accessTokenURL  = [NSURL URLWithString:QQ_ACCESS_TOKEN_URL];
		_engine.authorizeURL    = [NSURL URLWithString:QQ_AUTHORIZE_URL];
*/
		//set up the key and secreat if already authorized
		
		
		NSUserDefaults *prefs   = [NSUserDefaults standardUserDefaults];

		if( [prefs stringForKey:@"Name"] == nil )
			[prefs setObject:NSLocalizedString(@"Default baby name",@"") forKey:@"Name"];
		if( [prefs doubleForKey:@"Birthday"] == 0 )
			[prefs setDouble:[[NSDate date] timeIntervalSince1970] forKey:@"Birthday"];
		if( [prefs integerForKey:@"Gender"] == 0 )
			[prefs setInteger:0 forKey:@"Gender"];
	}
    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];
}


/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
*/

/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
*/


- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

- (void)loadDefaultKey {
	self.appKey      = [[NSUserDefaults standardUserDefaults] valueForKey:AppKey];
	self.appSecret   = [[NSUserDefaults standardUserDefaults] valueForKey:AppSecret];
	self.appKey      = SINA_APP_KEY;
	self.appSecret   = SINA_APP_SECRET;
	self.bindName    = @"SINA";
	//self.appKey      = TECENT_APP_KEY;
	//self.appSecret   = TECENT_APP_SECRET;
	//self.bindName    = @"QQ";
	self.tokenKey    = [[NSUserDefaults standardUserDefaults] valueForKey:AppTokenKey];
	self.tokenSecret = [[NSUserDefaults standardUserDefaults] valueForKey:AppTokenSecret];
	self.bindStatus  = [[NSUserDefaults standardUserDefaults] valueForKey:BindStatus];
}

- (void)parseTokenKeyWithResponse:(NSString *)aResponse {
	NSDictionary *params = [NSURL parseURLQueryString:aResponse];
	self.tokenKey = [params objectForKey:@"oauth_token"];
	self.tokenSecret = [params objectForKey:@"oauth_token_secret"];
	NSString *tmp = [params objectForKey:@"user_id"];
}

- (void)saveDefaultKey {
	
	self.bindStatus   = @"OK";
	[[NSUserDefaults standardUserDefaults] setValue:self.appKey forKey:AppKey];
	[[NSUserDefaults standardUserDefaults] setValue:self.appSecret forKey:AppSecret];
	[[NSUserDefaults standardUserDefaults] setValue:self.tokenKey forKey:AppTokenKey];
	[[NSUserDefaults standardUserDefaults] setValue:self.tokenSecret forKey:AppTokenSecret];
	[[NSUserDefaults standardUserDefaults] setValue:self.bindStatus forKey:BindStatus];
	[[NSUserDefaults standardUserDefaults] synchronize];
}
@end